from threading import Thread
from queue import Queue
import time

queue = Queue(maxsize=100)
TOTAL = 0
class BreadProducer(Thread):
    """_summary_
    面包生产者线程类
    Args:
        Thread (_type_): _description_
    """
    def __init__(self, name: str, count: int, queue: Queue) -> None:
        super().__init__()
        
        self.name = name
        self.queue = queue
        self.count = count
        
    def run(self) -> None:
        global TOTAL
        bread: str = "bread"
        while self.count >= 0:
            self.queue.put(bread, block=True)
            print(f"{self.name}生产了面包, 当前有{self.queue.qsize()}个面包, 总生产面包数为:{TOTAL}")
            self.count -= 1
            TOTAL += 1
            time.sleep(1)


class BreadConsumer(Thread):
    """_summary_面包消费者

    Args:
        Thread (_type_): _description_
    """
    def __init__(self, name: str, queue: Queue) -> None:
        super().__init__()
        self.name = name
        self.queue = queue
        self.daemon = True
        
    def run(self) -> None:
        while True:
            bread = self.queue.get(block=True)
            print(f"{self.name}消费了面包, 当前面包数为{self.queue.qsize()}")
            time.sleep(0.5)
            
            
threads = [
    BreadProducer(name="p1",queue=queue, count=100),
    BreadProducer(name="p2",queue=queue, count=100),
    BreadProducer(name="p3",queue=queue, count=100),
    
    BreadConsumer(name="c1",queue=queue),
    BreadConsumer(name="c2",queue=queue),
]

for t in threads:
    t.start()